package com.netease.service.impl;

import com.github.pagehelper.PageHelper;
import com.netease.service.UserDictService;
import com.netease.bo.UserDictBO;
import com.netease.mapper.UserDictMapper;
import com.netease.pojo.UserDict;
import com.netease.utils.DateUtil;
import com.netease.utils.FileUtils;
import com.netease.utils.PagedGridResult;
import org.n3r.idworker.Sid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @projectName: modeling-dev
 * @package: com.netease.impl
 * @className: WordSegmentLogService
 * @author: aochong
 * @description: 用户词典服务实现类
 * @date: 2023/4/3 19:49
 * @version: 1.0
 */
@Service
public class UserDictServiceImpl implements UserDictService {

    private static final Logger logger = LoggerFactory.getLogger(UserDictServiceImpl.class);
    private static final String PATH = "user.dict";

    @Autowired
    private UserDictMapper userDictmapper;

    @Autowired
    private Sid sid;

    @Override
    public void createUserDict(UserDict userDict) {

        //首次运行代码
        /*List<String> list = FileLoadUtils.ReadLineFromFile(new File(PATH));
        for (String line : list) {
            UserDict userDict1 = new UserDict();
            String[] fileds = line.split(" ");
            userDict1.setId(sid.nextShort());
            userDict1.setCstmDataElmnt(fileds[0]);
            userDict1.setWordFrqncy(fileds[1]);
            userDict1.setPartOfSpch(fileds[2]);
            userDict1.setInsertTime(DateUtil.dateToString(new Date()));
            userDict1.setUpdateTime(DateUtil.dateToString(new Date()));
            userDictmapper.insert(userDict1);
        }*/

        UserDict userDict1 = new UserDict();
        userDict1.setId(sid.nextShort());
        userDict1.setCstmDataElmnt(userDict.getCstmDataElmnt());
        userDict1.setWordFrqncy(userDict.getWordFrqncy());
        userDict1.setPartOfSpch(userDict.getPartOfSpch());
        userDict1.setInsertTime(DateUtil.dateToString(new Date()));
        userDict1.setUpdateTime(DateUtil.dateToString(new Date()));

        userDictmapper.insert(userDict1);
    }

    @Override
    public void updateUserDict(UserDictBO userDictBO) {

        Example example = new Example(UserDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cstmDataElmnt",userDictBO.getCstmDataElmnt());
        UserDict userDict = userDictmapper.selectOneByExample(example);
        userDict.setCstmDataElmnt(userDictBO.getCstmDataElmnt());
        userDict.setWordFrqncy(userDictBO.getWordFrqncy());
        userDict.setPartOfSpch(userDictBO.getPartOfSpch());
        userDict.setUpdateTime(DateUtil.dateToString(new Date()));

        userDictmapper.updateByPrimaryKey(userDict);
    }

    @Override
    public UserDict queryUserDict(String cstmDataElmnt) {
        Example example = new Example(UserDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cstmDataElmnt", cstmDataElmnt);

        return userDictmapper.selectOneByExample(example);
    }

    @Override
    public boolean queryUserDictIsExists(String cstmDataElmnt) {
        Example example = new Example(UserDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cstmDataElmnt", cstmDataElmnt);

        return userDictmapper.selectOneByExample(example) != null;
    }

    @Override
    public PagedGridResult queryPagedUserDict(Integer page, Integer pageSize) {
        /**
         * page: 第几页
         * pageSize: 每页显示条数
         */
        PageHelper.startPage(page, pageSize);
        List<UserDict> userDicts = userDictmapper.selectAll();
        PagedGridResult pagedGridResult = RootInfoServiceImpl.setterPagedGrid(userDicts, page);

        return pagedGridResult;
    }

    @Override
    public boolean deleteUserDict(String cstmDataElmnt) {
        System.out.println(userDictmapper.deleteByPrimaryKey(cstmDataElmnt));
        return false;
    }

    @Scheduled(cron ="0 * * * * ?") //每分钟执行一次落盘操作
    public void loadUserDictToFile(){
        List<UserDict> userDicts = userDictmapper.selectAll();
        ArrayList<String> list = new ArrayList<>();
        for (UserDict userDict : userDicts) {
            list.add(userDict.getCstmDataElmnt() + " " + userDict.getWordFrqncy() + " " + userDict.getPartOfSpch());
        }

        String path = new File(PATH).getAbsolutePath();

        System.out.println("定时任务开始执行了！！！");
        FileUtils.writeFile(list ,path);
    }

}
